import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/global/global_api.dart';
import 'package:flutter_app/icon/constant_icons.dart';
import 'package:flutter_app/main/content/content.dart';
import 'package:flutter_app/main/content/model/content_model.dart';
import 'package:flutter_app/main/main/model/project_table_list_entity.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class ProjectChildWidget extends StatefulWidget {

  int id;

  ProjectChildWidget(this.id, {super.key});

  @override
  ProjectChildPage createState() => ProjectChildPage(id: id);
}

class ProjectChildPage extends State<ProjectChildWidget> with AutomaticKeepAliveClientMixin{

  RefreshController controller = RefreshController(initialRefresh: false);
  int id;
  int pageIndex = 0;
  bool isRefresh = true;
  List<ProjectTableListDataDatas> dataList = [];

  ProjectChildPage({required this.id}) : super();

  void onRefresh() async {
    isRefresh = true;
    pageIndex = 0;
    getListData();
  }

  void onLoading() async {
    isRefresh = false;
    pageIndex++;
    getListData();
  }

  Future<void> getListData() async {
    String url = GlobalApi.getPublicListUrl(id.toString(), pageIndex);
    print("发起请求项目列表 $url");
    Response response = await Dio().get(url);
    var data = response.data;
    ProjectTableListEntity tableListEntity = ProjectTableListEntity.fromJson(data);
    setState(() {
      if (isRefresh) {
        dataList.clear();
        dataList.addAll(tableListEntity.data.datas);
        controller.refreshCompleted();
      } else {
        dataList.addAll(tableListEntity.data.datas);
        controller.loadComplete();
      }
    });
  }

  @override
  void initState() {
    super.initState();
    onRefresh();
  }

  @override
  Widget build(BuildContext context) {

    return SmartRefresher(
      controller: controller,
      enablePullDown: true,
      enablePullUp: true,
      header: const MaterialClassicHeader(),
      onRefresh: onRefresh,
      onLoading: onLoading,
      child: ListView.builder(
        itemBuilder: (c, i) => ListTile(
          onTap: () {
            Navigator.pushNamed(c, ContentWidget.RouteName,
                arguments: ContentModel(dataList[i].link));
          },
          enabled: true,
          title: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
                child: Text(
                  dataList[i].shareUser,
                  style: const TextStyle(color: Colors.black54, fontSize: 12),
                ),
              ),
              Container(
                margin: const EdgeInsets.fromLTRB(0, 5, 0, 0),
                child: Text(dataList[i].title,
                    style:
                    const TextStyle(color: Colors.black87, fontSize: 14)),
              ),
              Container(
                margin: const EdgeInsets.fromLTRB(0, 5, 0, 0),
                padding: const EdgeInsets.only(bottom: 10),
                child: Text(
                    '${dataList[i].chapterName}/${dataList[i].superChapterName}',
                    style:
                    const TextStyle(color: Colors.black54, fontSize: 12)),
              ),
            ],
          ),
          trailing: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(dataList[i].niceDate,
                  style:
                  const TextStyle(fontSize: 12, color: Colors.black54)),
              const SizedBox(
                height: 21,
                width: 10,
              ),
              const Icon(
                CustantIcons.squareCollectNoSelect,
                size: 20,
              ),
            ],
          ),
        ),
        itemCount: dataList.length,
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}